package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysSalary;
import com.ruoyi.system.domain.vo.SysSalaryVo;
import com.ruoyi.system.mapper.SysSalaryMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 工资发放记录Service业务层处理
 *
 * @author Mredust
 * @date 2025-05-27
 */
@Service
public class SysSalaryServiceImpl implements ISysSalaryService {
    @Autowired
    private SysSalaryMapper sysSalaryMapper;
    
    @Autowired
    private SysUserMapper sysUserMapper;
    
    /**
     * 查询工资发放记录
     *
     * @param id 工资发放记录主键
     * @return 工资发放记录
     */
    @Override
    public SysSalary selectSysSalaryById(Long id) {
        return sysSalaryMapper.selectSysSalaryById(id);
    }
    
    /**
     * 查询工资发放记录列表
     *
     * @param sysSalary 工资发放记录
     * @return 工资发放记录
     */
    @Override
    public List<SysSalaryVo> selectSysSalaryList(SysSalaryVo sysSalary) {
        return sysSalaryMapper.selectSysSalaryList(sysSalary);
    }
    
    /**
     * 新增工资发放记录
     *
     * @param sysSalary 工资发放记录
     * @return 结果
     */
    @Override
    public int insertSysSalary(SysSalaryVo sysSalary) {
        Date payDate = sysSalary.getPayDate();
        BigDecimal total = sysSalary.getTotal();
        String nickName = sysSalary.getNickName();
        String remark = sysSalary.getRemark();
        
        SysUser sysUser = sysUserMapper.selectUserByNickName(nickName);
        if (sysUser == null) {
            throw new RuntimeException("员工信息不存在");
        }
        Long userId = sysUser.getUserId();
        SysSalary salary = new SysSalary();
        salary.setPayDate(payDate);
        salary.setTotal(total);
        salary.setUserId(userId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String createUserNickName = loginUser.getUser().getNickName();
        salary.setCreateBy(createUserNickName);
        salary.setCreateTime(DateUtils.getNowDate());
        salary.setRemark(remark);
        
        return sysSalaryMapper.insertSysSalary(salary);
    }
    
    /**
     * 修改工资发放记录
     *
     * @param sysSalary 工资发放记录
     * @return 结果
     */
    @Override
    public int updateSysSalary(SysSalary sysSalary) {
        return sysSalaryMapper.updateSysSalary(sysSalary);
    }
    
    /**
     * 批量删除工资发放记录
     *
     * @param ids 需要删除的工资发放记录主键
     * @return 结果
     */
    @Override
    public int deleteSysSalaryByIds(Long[] ids) {
        return sysSalaryMapper.deleteSysSalaryByIds(ids);
    }
    
    /**
     * 删除工资发放记录信息
     *
     * @param id 工资发放记录主键
     * @return 结果
     */
    @Override
    public int deleteSysSalaryById(Long id) {
        return sysSalaryMapper.deleteSysSalaryById(id);
    }
}
